'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN',
        'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function() {
        this.initEdit();
        this.initModal();
        this.refund();
        this.initInvoice();
        this.initPay();
        this.initDistributionChannel();
    },
    
    initEdit: function(){
        $(document).on("shown.bs.modal", "#remark", function(e) {
        var $form = $(e.currentTarget).find("form");
      
        $form.bt_validate();
      
        $form.shiftform({
            beforeSubmit: function(el) {
              $('form').trigger("validate.ajax.submit");
                   return $.bt_validate.result[$.bt_validate.form_id]
            }
        
        });
        
        })
        
    },
    
    initModal: function(){
        $(document).on('shown.bs.modal', '#close', function(e){

            $(e.currentTarget).find("form").shiftform()
            })
        
    },
    
    refund: function(){
        $(document).on("shown.bs.modal", "#refund", function(e) {
        
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
        })
        
    },
    
    initInvoice: function(){
        $(document).on("shown.bs.modal", "#invoice", function(e) {
        
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
        })
        
    },
    
    initPay: function(){
        $(document).on("shown.bs.modal", "#pay", function(e) {
            
              $('select[name="paymentMethod"]').on('change', function() {
            	    var $orderNumber = $('input[name="orderNumber"]');
                    $('.order-number').addClass('hide');
                    $orderNumber.removeAttr("data-validate").val("");
                    var paymentMethod = $(this).val();
                    if(paymentMethod == 'TAOBAO') {
                    	$('.order-number').removeClass('hide');
                    	$orderNumber.attr("data-validate", "required");
                    }
                        
                })
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        
            })
        
        },
        
        initDistributionChannel: function(){
            $(document).on("shown.bs.modal", "#distribution-channel", function(e) {
            
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                  
                  var $form = $(e.currentTarget).find("form")                          //bind shiftform
                  $form.shiftform({
                    beforeSubmit : function(el) {
                      $form.trigger("validate.ajax.submit");
                      return $.bt_validate.result[$.bt_validate.form_id]
                    }
                  })
            })
            
        }
    
   }
})
